/*
 * @lc app=leetcode.cn id=653 lang=typescript
 *
 * [653] 两数之和 IV - 输入二叉搜索树
 */

// @lc code=start
/**
 * Definition for a binary tree node.
 * class TreeNode {
 *     val: number
 *     left: TreeNode | null
 *     right: TreeNode | null
 *     constructor(val?: number, left?: TreeNode | null, right?: TreeNode | null) {
 *         this.val = (val===undefined ? 0 : val)
 *         this.left = (left===undefined ? null : left)
 *         this.right = (right===undefined ? null : right)
 *     }
 * }
 */

function findTarget(root: TreeNode | null, k: number): boolean {
    if (!root) {
        return false;
    }
    const set = new Set();
    return loop(root, k, set)
};

function loop(root: TreeNode | null, k: number, set: any) {
    if (!root) {
        return false
    }
    if (set.has(k - root.val)) {
        return true;
    } else {
        set.add(root.val)
    }

    return loop(root.left, k, set) || loop(root.right, k, set)

}
// @lc code=end

